﻿using Common.Attributes;
using MODEL;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Admin
{
    public class RecPositionController : Controller
    {

        //要实现的方法有
        //1、获取校园招聘会岗位列表视图GetRecPositionData()
        //2、修改edit+新增+删除+
        //3、审核通过+岗位详细信息+查看+高级搜索



        #region 校园招聘会岗位视图+ActionResult index()
        /// <summary>
        /// 校园招聘会岗位视图
        /// </summary>
        /// <returns></returns>
        public ActionResult index()
        {
            return View();
        }
        #endregion

        #region 获取校园招聘会岗位列表数据 +GetRecPositionData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取岗位列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRecPositionData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //qgzx_recruit_notice model = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.GetListBy(x => x.IsOpen == 1).FirstOrDefault();
            //int flag = model.Id;
            var list = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetPagedList(pageIndex, pageSize, x => x.IsDel != 1 && x.RecruitmentId > 0, m => m.PositionId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId != null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 在这新增学岗位 html代码 + AddRecPosition()
        /// <summary>
        /// 在这新增学生员 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddRecPosition()
        {
            return PartialView("EditRecPosition");
        }
        #endregion

        #region 在这新增岗位 html代码 + AddRecPosition(MODEL.qgzx_recruit_position model)
        /// <summary>
        /// 在这新增岗位 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddRecPosition(MODEL.qgzx_position model)
        {
            //新增时，先查看校园招聘会是否开启，若在开启状态，则将这一届招聘会的id传送给RecruitmentId；
            qgzx_recruit_notice recruit_model = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.GetListBy(x => x.IsOpen == 1).FirstOrDefault();
            model.RecruitmentId = recruit_model.Id;
            int res = OperateContext.Current.BLLSession.Iqgzx_positionBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/admin/RecPosition/index?ok");
            }
            return Redirect("/admin/RecPosition/index?err");
        }
        #endregion

        #region 删除岗位 + DelRecPosition()
        /// <summary>
        /// 删除岗位 + DelRecPosition()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelRecPosition()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                qgzx_position model = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId == id).FirstOrDefault();
                model.IsDel = 1;
                int r = OperateContext.Current.BLLSession.Iqgzx_positionBLL.Modify(model, "CompanyId", "PositionName", "PositionType", "ReleaseDate", "Deadlines", "AuditStatus", "IsBlackList", "IsDel", "IsShow", "RecruitmentId");
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion
        
        #region 加载修改岗位 window 的html + EditRecPosition()
        /// <summary>
        /// 加载修改学生 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditRecPosition(int id)
        {

            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId == id).FirstOrDefault().ToViewModel();
            string CompanyName = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyId == model.CompanyId).FirstOrDefault().CompanyName;
            ViewBag.CompanyName = CompanyName;
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改岗位信息 EditRecPosition(MODEL.qgzx_position model)
        /// <summary>
        ///post修改学生信息 EditRecPosition(MODEL.qgzx_recruit_position model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditRecPosition(MODEL.qgzx_position model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_positionBLL.Modify(model, "CompanyId", "CompanyName", "PositionTitle", "PositionName", "PositionType", "Treatment",
                "ReleaseDate", "Deadlines", "RecruitNum", "RequestGender", "WorkAddress", "WorkTime", "PositionDescrip", "AuditStatus", "IsShow", "OnCompanyOwn", "AuditReason", "IsPoor");
            if (res > 0)
            {
                return Redirect("/admin/RecPosition/index");
            }
            return Redirect("/admin/RecPosition/index");
        }
        #endregion

        #region 查看申请学生 + ActionResult JoinStu(int id)
        /// <summary>
        /// 查看参与学生
        /// </summary>
        /// <param name="id">学生id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult JoinStu(int id)
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //qgzx_position mode =OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.RecruitmentId>0).FirstOrDefault();

            var list = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetPagedList( pageIndex, pageSize,x => x.PositionId == id,x=>x.PosApplyId);
    
                ////查询参与岗位同学定规格字符串到List<int>中
                //List<int> RecJoinStuId = model.RecJoinStudentsId.Split(new string[1] { "|" }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
                //到学生申请表中查询所有申请学生返回
                //var list = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(m => model.PosApplyId != null);


                var rowCount = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(m => m.PosApplyId != null).Count;
                //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");


                MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
                {
                    total = rowCount,
                    rows = list,
                    footer = null
                };

                return Json(dgMode);
            }
       
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string CompanyId = Request.QueryString["CompanyId"];
            string PositionName = Request.QueryString["PositionName"];
            string PositionType = Request.QueryString["PositionType"];
            string AuditStatus = Request.QueryString["AuditStatus"];
            string IsBlackList = Request.QueryString["IsBlackList"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId != null).ToList();
            if (CompanyId != "")
            {
                list = list.Where(x => x.CompanyId.ToString().Contains(CompanyId)).ToList();
            }
            if (PositionName != "")
            {
                list = list.Where(x => x.PositionName.Contains(PositionName)).ToList();
            }
            if (PositionType != "")
            {
                list = list.Where(x => x.PositionType.Contains(PositionType)).ToList();
            }
            if (AuditStatus != "")
            {
                list = list.Where(x => x.AuditStatus.ToString().Contains(AuditStatus)).ToList();
            }
           
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 岗位详细信息 + ActionResult RecDetail()
        /// <summary>
        /// 岗位详细信息 + ActionResult Detail(int id)
        /// </summary>
        /// <returns></returns>
        public ActionResult RecDetail(int id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId == id).FirstOrDefault().ToViewModel();

            return View(model);
        }
        #endregion

        //-------------------------------单位筛选学生和管理员审核-------------------------------------------

        //#region 商家筛选上岗学生 + CompanyChooseStu(int id)
        ///// <summary>
        /////  商家筛选上岗学生
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public ActionResult CompanyChooseStu(int id)
        //{
        //    int posApplyId = int.Parse(Request.Form["PosApplyId"]);
        //    qgzx_position_application model = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(x => x.PosApplyId == posApplyId).FirstOrDefault();
        //    model.DistriResult = 1;

        //    OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.ModifyBy(model, x => x.PosApplyId == model.PosApplyId, "DistriResult");
        //    return OperateContext.Current.RedirectAjax("ok", "审核通过~", null, "");
        //}
        //#endregion

        #region 商家筛选上岗学生 + ActionResult CompanyAuditStu()
        /// <summary>
        ///  商家筛选上岗学生
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult CompanyAuditStu(int id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(x => x.PosApplyId == id).FirstOrDefault().ToViewModel();

            return View(model);
        }
        #endregion

        #region post  单位筛选上岗学生  CompanyAuditStu(MODEL.qgzx_position_application model)
        /// <summary>
        ///单位筛选上岗学生
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult CompanyAuditStu(MODEL.qgzx_position_application model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.Modify(model, "PosApplyId", "DistriResult", "DistriReason");
           
            if (res > 0)
            {
                return Redirect("/admin/RecPosition/JoinStu");
            }
            return Redirect("/admin/RecPosition/JoinStu");
        }
        #endregion




        #region 单位筛选后，管理员审核上岗学生 + ActionResult AdminAuditStu()
        /// <summary>
        ///  单位筛选后，管理员审核上岗学生
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AdminAuditStu(int id)
        {
            var model = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.GetListBy(x => x.PosApplyId == id).FirstOrDefault().ToViewModel();

            return View(model);
        }
        #endregion

        #region post  单位筛选后，管理员审核上岗学生  AdminAuditStu(MODEL.qgzx_position_application model)
        /// <summary>
        ///单位筛选后，管理员审核上岗学生
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AdminAuditStu(MODEL.qgzx_position_application model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_position_applicationBLL.Modify(model, "PosApplyId", "AuditResult", "AuditTime","AuditReason","IsOnGuard");

            if (res > 0)
            {
                return Redirect("/admin/RecPosition/JoinStu");
            }
            return Redirect("/admin/RecPosition/JoinStu");
        }
        #endregion
        //AdminAuditStu

        //-------------------------------------------------------------------------------------------------------


        //----------------------------------业务方法---------------------------------------------
        #region [HttpGet]发布岗位信息填写视图 +  ReleasePosition()
        /// <summary>
        /// [HttpGet]发布岗位信息填写视图 +  ReleasePosition()
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ReleasePosition()
        {
            qgzx_admin current = OperateContext.Current.Admin;
            qgzx_company model = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyName == current.LoginName).FirstOrDefault();
            if (model == null)
            {
                return OperateContext.Current.RedirectAjax("err", "没有权限或者不属于公司单位", null, "");
            }
            else
            {
                return View();
            }
        }
        #endregion

        #region 发布岗位 业务 + ReleasePosition(MODEL.qgzx_position model)
        /// <summary>
        ///发布岗位 业务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        [AjaxRequest]
        public ActionResult ReleasePosition(MODEL.qgzx_position model)
        {
            qgzx_recruit_notice res = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.GetListBy(x => x.IsOpen == 1).FirstOrDefault();

            if (res != null)
            {

                int CompareId = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyName == OperateContext.Current.Admin.LoginName).First().CompanyId;

                model.CompanyId = CompareId;
                model.ReleaseDate = DateTime.Now;
                model.RecruitmentId = res.Id;
                OperateContext.Current.BLLSession.Iqgzx_positionBLL.Add(model);
                return OperateContext.Current.RedirectAjax("ok", "发布岗位成功，请等待审核", null, "");
            }
            else
            {
                return OperateContext.Current.RedirectAjax("ok", "校园招聘会时间未到，敬请留意网站发布的最新动态", null, "");
            }
        }
        #endregion
    }
}
